﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RaytracerLib.MathLib;
using TomanuExtensions;

namespace RaytracerLib.UVMappers
{
    public class CylindricalUVMapper : UVMapper
    {
        public override Vector2 Map(Vector3 a_uvw)
        {
            if (a_uvw.Y.IsAlmostEquals(0) || a_uvw.Y.IsAlmostEquals(1))
                return new Vector2(a_uvw.X, a_uvw.Z);
            else
            {
                double u = Math.Atan2(a_uvw.X - 0.5, a_uvw.Z - 0.5) / 2 / MathExtensions.PI;

                if (a_uvw.X < 0.5)
                    u = u + 1;

                return new Vector2(u, a_uvw.Y);
            }
        }

        public override UVMappingMethod Method
        {
            get
            {
                return UVMappingMethod.Cylindrical;
            }
        }
    }
}
